﻿@{
    // Set the layout page and page title
    Layout = "~/tpl/_layout.cshtml";
    PageData["PageTitle"] = "Log In";
    
    // Initialize general page variables
    var username = "";
    var password = "";
    var rememberMe = false;

    // Validation
    var isValid = true;
    var errors = new Dictionary<string,string>();

    // If this is a POST request, validate and process data
    if (IsPost) 
    {
        username = Request.Form["username"];
        password = Request.Form["password"];
        rememberMe = Request.Form["rememberMe"].AsBool();
        
        // Validate the user's username
        if (username.IsEmpty()) 
        {
            errors.Add("usernameErrorMessage", "You must specify a username.");
            isValid = false;
        }
        
        // Validate the user's password
        if (password.IsEmpty()) 
        {
            errors.Add("passwordErrorMessage", "You must specify a password.");
            isValid = false;
        }
        
        // Confirm there are no validation errors
        if (isValid) 
        {
            // Attempt to login to the Security object using provided creds
            if (WebSecurity.Login(username, password, rememberMe)) 
            {
                if (Request["ReturnUrl"] != null)
                {
                    Response.Redirect("~" + Request["ReturnUrl"]);
                }
                
                Response.Redirect("~/");
            }
            
            // If we arrived here, the login failed; convey that to the user
            isValid = false;
        }    
    }
}
@* If one or more validation errors exist, show an error *@
@if (!isValid) 
{
   <div>There was a problem with your login and/or errors exist in your form.</div>
}

<fieldset>
    <legend>Log In to Your Account</legend>
    <form method="post" action="">
        <div>
            <label for="username" id="labelUsername">Username:</label>
            <input type="text" id="username" name="username" value="@username" />
            @if (errors.ContainsKey("usernameErrorMessage")) 
            {
                <span>@errors["usernameErrorMessage"]</span>
            }
        </div>         
        <div>
            <label for="password" id="labelPassword">Password:</label>
            <input type="password" id="password" name="password" value="" />
            @if (errors.ContainsKey("passwordErrorMessage") )
            {
                <span>@errors["passwordErrorMessage"]</span>
            }
        </div>
        <div>
            <label for="rememberMe" id="labelRememberMe">Remember Me?</label>
            <input type="checkbox" id="rememberMe" name="rememberMe" @(rememberMe ? @" checked=""checked""" : "") />
        </div>
        <div>
            <input type="submit" value="Login" />
        </div>
        <div>
            <a href="ForgotPassword">Did you forget your password?</a>
        </div>
    </form>
</fieldset>